package org.mch.swingm.core;

import java.awt.BorderLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;

public class MListToList<E> extends JPanel implements ActionListener, ListSelectionListener {
	private static final long serialVersionUID = 3717340988460279863L;

	private JLabel ivjlblLeft = null;
	private JLabel ivjlblRight = null;
	private JButton ivjbnL = null;
	private JButton ivjbnLL = null;
	private JButton ivjbnR = null;
	private JButton ivjbnRR = null;
	private boolean isDisplayTitle = false;
	private JPanel ivjJPane1 = null;
	private JPanel ivjJPane2 = null;
	private String fieldLeftText = new String();
	private String fieldRightText = new String();
	private JList<E> ivjLstLeft = null;
	private JList<E> ivjLstRight = null;
	private boolean fieldEnabled = true;
	private HashMap<JButton, Boolean> m_hEnabled = null;

	public MListToList() {
		super();
		initialize();
	}

	public MListToList(LayoutManager lm) {
		super(lm);
	}

	public MListToList(LayoutManager lm, boolean bl) {
		super(lm, bl);
	}

	public MListToList(boolean bl) {
		super(bl);
	}

	public void actionPerformed(java.awt.event.ActionEvent e) {
		if ((e.getSource() == getbnR())) {
			onR();
		} else if ((e.getSource() == getbnRR())) {
			onRR();
		} else if ((e.getSource() == getbnL())) {
			onL();
		} else if ((e.getSource() == getbnLL())) {
			onLL();
		}
	}

	public JButton getbnL() {
		if (ivjbnL == null) {
			try {
				ivjbnL = new JButton();
				ivjbnL.setName("bnL");
				ivjbnL.setText("<");
				ivjbnL.setBounds(201, 157, 40, 25);
				ivjbnL.setMargin(new java.awt.Insets(2, 0, 2, 0));
			} catch (Throwable e) {
				logError(e);
			}
		}
		return ivjbnL;
	}

	public JButton getbnLL() {
		if (ivjbnLL == null) {
			try {
				ivjbnLL = new JButton();
				ivjbnLL.setName("bnLL");
				ivjbnLL.setText("<<");
				ivjbnLL.setBounds(201, 206, 40, 25);
				ivjbnLL.setMargin(new java.awt.Insets(2, 0, 2, 0));
			} catch (Throwable e) {
				logError(e);
			}
		}
		return ivjbnLL;
	}

	public JButton getbnR() {
		if (ivjbnR == null) {
			try {
				ivjbnR = new JButton();
				ivjbnR.setName("bnR");
				ivjbnR.setText(">");
				ivjbnR.setBounds(201, 66, 40, 25);
				ivjbnR.setMargin(new java.awt.Insets(2, 0, 2, 0));
			} catch (Throwable e) {
				logError(e);
			}
		}
		return ivjbnR;
	}

	public JButton getbnRR() {
		if (ivjbnRR == null) {
			try {
				ivjbnRR = new JButton();
				ivjbnRR.setName("bnRR");
				ivjbnRR.setText(">>");
				ivjbnRR.setBounds(201, 110, 40, 25);
				ivjbnRR.setMargin(new java.awt.Insets(2, 0, 2, 0));
			} catch (Throwable e) {
				logError(e);
			}
		}
		return ivjbnRR;
	}

	private boolean getButtonEnabled(JButton button) {
		if (m_hEnabled != null && m_hEnabled.containsKey(button))
			return ((Boolean) m_hEnabled.get(button)).booleanValue();

		return true;
	}

	private JLabel getlblLeft() {
		if (ivjlblLeft == null) {
			try {
				ivjlblLeft = new JLabel();
				ivjlblLeft.setName("lblLeft");
				ivjlblLeft.setText("JLabel1");
				ivjlblLeft.setBounds(1, 2, 173, 22);
				ivjlblLeft.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
				ivjlblLeft.setText(fieldLeftText);
				ivjlblLeft.setVisible(isDisplayTitle());
			} catch (Throwable e) {
				logError(e);
			}
		}
		return ivjlblLeft;
	}

	public void logError(Throwable e) {
		e.printStackTrace();
	}

	private JLabel getlblRight() {
		if (ivjlblRight == null) {
			try {
				ivjlblRight = new JLabel();
				ivjlblRight.setName("lblRight");
				ivjlblRight.setText("JLabel2");
				ivjlblRight.setBounds(263, 5, 158, 27);
				ivjlblRight.setText(fieldRightText);
				ivjlblRight.setVisible(isDisplayTitle());
			} catch (Throwable e) {
				logError(e);
			}
		}
		return ivjlblRight;
	}

	public Object[] getLeftData() {
		int iLen = ((DefaultListModel<E>) getLstLeft().getModel()).getSize();
		ArrayList<Object> os = new ArrayList<Object>();
		Object o = null;
		for (int i = 0; i < iLen; i++) {
			o = ((DefaultListModel<E>) getLstLeft().getModel()).getElementAt(i);
			os.add(o);
		}
		if (os != null && os.size() > 0)
			return os.toArray();
		else
			return null;
	}

	public java.lang.String getLeftText() {
		return fieldLeftText;
	}

	public JList<E> getLstLeft() {
		if (ivjLstLeft == null) {
			try {
				ivjLstLeft = new JList<E>();
				ivjLstLeft.setName("LstLeft");
				ivjLstLeft.setBounds(0, 0, 170, 224);
				ivjLstLeft.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
			} catch (Throwable e) {
				logError(e);
			}
		}
		return ivjLstLeft;
	}

	public JList<E> getLstRight() {
		if (ivjLstRight == null) {
			try {
				ivjLstRight = new JList<E>();
				ivjLstRight.setName("LstRight");
				ivjLstRight.setBounds(0, 0, 175, 210);
				ivjLstRight.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
			} catch (Throwable e) {
				logError(e);
			}
		}
		return ivjLstRight;
	}

	public Object[] getRightData() {
		int iLen = ((DefaultListModel<E>) getLstRight().getModel()).getSize();
		ArrayList<Object> os = new ArrayList<Object>();
		Object o = null;
		for (int i = 0; i < iLen; i++) {
			o = ((DefaultListModel<E>) getLstRight().getModel()).getElementAt(i);
			os.add(o);
		}
		if (os != null && os.size() > 0)
			return os.toArray();
		else
			return null;
	}

	public java.lang.String getRightText() {
		return fieldRightText;
	}

	private JPanel getJScrollPane1() {
		if (ivjJPane1 == null) {
			ivjJPane1 = new JPanel(new BorderLayout());
			ivjJPane1.setBounds(0, 30, 176, 227);
			JScrollPane ivjJScrollPane1 = new JScrollPane();
			ivjJScrollPane1.setViewportView(getLstLeft());
			ivjJPane1.add(Box.createHorizontalStrut(3), BorderLayout.WEST);
			ivjJPane1.add(ivjJScrollPane1, BorderLayout.CENTER);
			ivjJPane1.add(Box.createVerticalStrut(3), BorderLayout.SOUTH);
		}
		return ivjJPane1;
	}

	private JPanel getJScrollPane2() {
		if (ivjJPane2 == null) {
			ivjJPane2 = new JPanel(new BorderLayout());
			ivjJPane2.setBounds(259, 30, 176, 227);
			JScrollPane ivjJScrollPane2 = new JScrollPane();
			ivjJScrollPane2.setViewportView(getLstRight());
			ivjJPane2.add(ivjJScrollPane2, BorderLayout.CENTER);
			ivjJPane2.add(Box.createVerticalStrut(3), BorderLayout.SOUTH);
			ivjJPane2.add(Box.createHorizontalStrut(3), BorderLayout.EAST);

		}
		return ivjJPane2;
	}

	private void initConnections() {
		getbnR().addActionListener(this);
		getbnRR().addActionListener(this);
		getbnL().addActionListener(this);
		getbnLL().addActionListener(this);
		getLstLeft().addListSelectionListener(this);
		getLstRight().addListSelectionListener(this);
	}

	private void initialize() {
		try {
			setName("JListToList");
			setLayout(null);
			setSize(515, 423);
			add(getlblLeft(), getlblLeft().getName());
			add(getlblRight(), getlblRight().getName());
			add(getbnR(), getbnR().getName());
			add(getbnRR(), getbnRR().getName());
			add(getbnL(), getbnL().getName());
			add(getbnLL(), getbnLL().getName());
			add(getJScrollPane1());
			add(getJScrollPane2());
		} catch (Throwable e) {
			logError(e);
		}

		initConnections();

		getLstLeft().setSelectionMode(1);
		getLstRight().setSelectionMode(1);
		getLstLeft().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		getLstRight().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		setLeftData(null);
		setRightData(null);
		setButtonsEnabled();
	}

	public boolean isDisplayTitle() {
		return isDisplayTitle;
	}

	public boolean isEnabled() {
		return fieldEnabled;
	}

	public void onL() {
		try {
			List<E> o = getLstRight().getSelectedValuesList();
			if (o == null)
				return;
			for (int i = 0; i < o.size(); i++) {
				((DefaultListModel<E>) getLstLeft().getModel()).addElement(o.get(i));
				((DefaultListModel<E>) getLstRight().getModel()).removeElement(o.get(i));
			}
			setButtonsEnabled();
			getLstRight().repaint();
			getLstLeft().repaint();
		} catch (Exception e) {
			logError(e);
		}
	}

	public void onLL() {
		try {
			E o = null;
			for (int i = 0; i < ((DefaultListModel<E>) getLstRight().getModel()).getSize(); i++) {
				o = ((DefaultListModel<E>) getLstRight().getModel()).getElementAt(i);
				((DefaultListModel<E>) getLstLeft().getModel()).addElement(o);
			}
			((DefaultListModel<E>) getLstRight().getModel()).removeAllElements();
			setButtonsEnabled();
			getLstRight().repaint();
			getLstLeft().repaint();
		} catch (Exception e) {
			logError(e);
		}
	}

	public void onR() {
		try {
			List<E> o = getLstRight().getSelectedValuesList();
			if (o == null)
				return;
			for (int i = 0; i < o.size(); i++) {
				((DefaultListModel<E>) getLstRight().getModel()).addElement(o.get(i));
				((DefaultListModel<E>) getLstLeft().getModel()).removeElement(o.get(i));
			}
			setButtonsEnabled();
			getLstRight().repaint();
			getLstLeft().repaint();
		} catch (Exception e) {
			logError(e);
		}
	}

	public void onRR() {
		try {
			E o = null;
			for (int i = 0; i < ((DefaultListModel<E>) getLstLeft().getModel()).getSize(); i++) {
				o = ((DefaultListModel<E>) getLstLeft().getModel()).getElementAt(i);
				((DefaultListModel<E>) getLstRight().getModel()).addElement(o);
			}
			((DefaultListModel<E>) getLstLeft().getModel()).removeAllElements();
			setButtonsEnabled();
			getLstRight().repaint();
			getLstLeft().repaint();
		} catch (Exception e) {
			logError(e);
		}
	}

	public void setBounds(int x, int y, int w, int h) {
		super.setBounds(x, y, w, h);

		int Width = w;
		int Hight = h;
		if ((Width - 70) % 2 == 1) {
			Width += 1;
		}
		while ((Hight - 100) % 5 != 0) {
			Hight += 1;
		}
		if (!isDisplayTitle) {
			getlblLeft().setVisible(false);
			getlblRight().setVisible(false);
			//
			getJScrollPane1().setBounds(0, 0, (Width - 70) / 2, Hight);
			getJScrollPane2().setBounds(Width - (Width - 70) / 2, 0, (Width - 70) / 2, Hight);
			getbnR().setBounds((Width - 70) / 2 + 15, (Hight - 100) / 5, 40, 25);
			getbnRR().setBounds((Width - 70) / 2 + 15, 2 * (Hight - 100) / 5 + 25, 40, 25);
			getbnL().setBounds((Width - 70) / 2 + 15, 3 * (Hight - 100) / 5 + 50, 40, 25);
			getbnLL().setBounds((Width - 70) / 2 + 15, 4 * (Hight - 100) / 5 + 75, 40, 25);
		} else {
			getlblLeft().setVisible(true);
			getlblRight().setVisible(true);

			getlblLeft().setBounds(0, 0, (Width - 70) / 2, 25);
			getlblRight().setBounds(Width - (Width - 70) / 2, 0, (Width - 70) / 2, 25);
			Hight -= 30;
			getJScrollPane1().setBounds(0, 0 + 30, (Width - 70) / 2, Hight);
			getJScrollPane2().setBounds(Width - (Width - 70) / 2, 0 + 30, (Width - 70) / 2, Hight);
			getbnR().setBounds((Width - 70) / 2 + 15, (Hight - 100) / 5 + 30, 40, 25);
			getbnRR().setBounds((Width - 70) / 2 + 15, 2 * (Hight - 100) / 5 + 25 + 30, 40, 25);
			getbnL().setBounds((Width - 70) / 2 + 15, 3 * (Hight - 100) / 5 + 50 + 30, 40, 25);
			getbnLL().setBounds((Width - 70) / 2 + 15, 4 * (Hight - 100) / 5 + 75 + 30, 40, 25);
		}
	}

	public void setButtonEnabled(JButton button, boolean canBeUsed) {
		button.setEnabled(canBeUsed);
		if (canBeUsed) {
			if (m_hEnabled != null && m_hEnabled.containsKey(button)) {
				m_hEnabled.remove(button);
			}
			return;
		}

		if (m_hEnabled == null)
			m_hEnabled = new HashMap<JButton, Boolean>();

		m_hEnabled.put(button, new Boolean(canBeUsed));
	}

	private void setButtonsEnabled() {
		if (getLstLeft().getModel().getSize() > 0) {
			getbnR().setEnabled(getButtonEnabled(getbnR()));
			getbnRR().setEnabled(getButtonEnabled(getbnRR()));
		} else {
			getbnR().setEnabled(false);
			getbnRR().setEnabled(false);
		}
		if (getLstRight().getModel().getSize() > 0) {
			getbnL().setEnabled(getButtonEnabled(getbnL()));
			getbnLL().setEnabled(getButtonEnabled(getbnLL()));
		} else {
			getbnL().setEnabled(false);
			getbnLL().setEnabled(false);
		}
	}

	public void setDisplayTitle(boolean b) {
		isDisplayTitle = b;
		setBounds(getBounds().x, getBounds().y, getBounds().width, getBounds().height);
	}

	public void setEnabled(boolean enabled) {
		boolean oldValue = fieldEnabled;
		fieldEnabled = enabled;
		getLstLeft().setEnabled(enabled);
		getLstRight().setEnabled(enabled);
		if (enabled) {
			setButtonsEnabled();
		} else {
			getbnL().setEnabled(enabled);
			getbnLL().setEnabled(enabled);
			getbnR().setEnabled(enabled);
			getbnRR().setEnabled(enabled);
		}
		firePropertyChange("enabled", new Boolean(oldValue), new Boolean(enabled));
	}

	public void setLeftData(E[] leftdata) {

		DefaultListModel<E> listModel = new DefaultListModel<E>();
		if (leftdata != null && leftdata.length > 0) {

			for (int i = 0; i < leftdata.length; i++)
				listModel.addElement(leftdata[i]);
			if (isEnabled()) {
				getbnR().setEnabled(getButtonEnabled(getbnR()));
				getbnRR().setEnabled(getButtonEnabled(getbnRR()));
			}
		} else {
			getbnR().setEnabled(false);
			getbnRR().setEnabled(false);
		}

		getLstLeft().setModel(listModel);
		getLstLeft().repaint();
	}

	public void setLeftText(java.lang.String leftText) {
		String oldValue = fieldLeftText;
		fieldLeftText = leftText;
		getlblLeft().setText(fieldLeftText);
		firePropertyChange("leftText", oldValue, leftText);
	}

	public void setRightData(E[] rightdata) {

		DefaultListModel<E> listModel = new DefaultListModel<E>();
		if (rightdata != null && rightdata.length > 0) {
			for (int i = 0; i < rightdata.length; i++)
				listModel.addElement(rightdata[i]);
			if (isEnabled()) {
				getbnL().setEnabled(getButtonEnabled(getbnL()));
				getbnLL().setEnabled(getButtonEnabled(getbnLL()));
			}
		} else {
			getbnL().setEnabled(false);
			getbnLL().setEnabled(false);
		}
		getLstRight().setModel(listModel);
		getLstRight().repaint();
	}

	public void setRightText(java.lang.String rightText) {
		String oldValue = fieldRightText;
		fieldRightText = rightText;
		getlblRight().setText(fieldRightText);
		firePropertyChange("rightText", oldValue, rightText);
	}

	public void valueChanged(javax.swing.event.ListSelectionEvent e) {
		if (e.getSource() == getLstLeft())
			valueChanged_left();
		if (e.getSource() == getLstRight())
			valueChanged_right();
	}

	public void valueChanged_left() {
		try {
			/*
			 * int iSelectIndex=-1;//ѡ������,<0Ϊδѡ�� if (listModel.size()>0)
			 * iSelectIndex=getlstItem().getSelectedIndex(); if
			 * (iSelectIndex>=0) { nc.vo.bm.bm_003.ItemVO
			 * singleData=(nc.vo.bm.bm_003
			 * .ItemVO)listModel.getElementAt(iSelectIndex); //��ʾ���
			 * voToDis(singleData); } else { //��ʾ�ռ�¼ voToDis(null); }
			 */
		} catch (Exception e) {
			logError(e);
		}
	}

	public void valueChanged_right() {
		try {
			/*
			 * int iSelectIndex=-1;//ѡ������,<0Ϊδѡ�� if (listModel.size()>0)
			 * iSelectIndex=getlstItem().getSelectedIndex(); if
			 * (iSelectIndex>=0) { nc.vo.bm.bm_003.ItemVO
			 * singleData=(nc.vo.bm.bm_003
			 * .ItemVO)listModel.getElementAt(iSelectIndex); //��ʾ���
			 * voToDis(singleData); } else { //��ʾ�ռ�¼ voToDis(null); }
			 */
		} catch (Exception e) {
			logError(e);
		}
	}
}
